using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;
public partial class Admin_SanPhamChiTiet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            int iD = int.Parse(Request.QueryString["iD"].ToString());
            lit_masp . Text = iD.ToString();
            tbSanPham sanpham = tbSanPhamBUS.GetByID(iD);
            lit_tensp . Text = sanpham.TenSP;
            tbDanhMuc danhmuc = tbDanhMucBUS.GetByID(sanpham.DanhMuc );
            lit_danhmuc.Text = danhmuc.TenDanhMuc.ToString();
            tbDoanhNghiep doanhnghiep = tbDoanhNghiepBUS.GetByID(sanpham.DoanhNghiep);
            lit_doanhnghiep . Text = doanhnghiep.TenDN .ToString();
            tbNhaSX nhasx = tbNhaSXBUS.GetByID(sanpham.NhaSX);
            lit_nhasx.Text = nhasx.TenNSX.ToString();
            lit_giagoc . Text = sanpham.GiaGoc.ToString();
            lit_giaban . Text = sanpham.GiaBan.ToString();
            tbDiaDiem diadiem = tbDiaDiemBUS.GetByID(sanpham.DiaDiem);
            lit_diadiem.Text = diadiem.TenDD.ToString();
            lit_ngaybatdau . Text = sanpham.NgayBatDau.ToString();
            lit_ngayhethan . Text = sanpham.NgayHetHan.ToString();
            lit_soluongban . Text = sanpham.SoLuongBan.ToString();
            lit_soluongtoida .Text = sanpham.SoLuongToiDa.ToString();
            lit_mota . Text = sanpham.MoTa;
            lit_quydinhsudung . Text = sanpham.QuyDinhSuDung;
            lit_chietkhau .  Text = sanpham.ChietKhau.ToString();
            tbTinhTrang tinhtrang = tbTinhTrangBUS.GetByID(sanpham.TinhTrang);
            lit_tinhtrang.Text = tinhtrang.TenTinhTrang.ToString();
            tbPhuongThucThanhToan phuongthucthanhtoan = tbPhuongThucThanhToanBUS.GetByID(sanpham.PhuongThucThanhToan);
            lit_phuongthucthanhtoan.Text = phuongthucthanhtoan.TenPTTT.ToString();
            lit_loigioithieu.Text = sanpham.LoiGioiThieu;
            Pager.PageSize = 5;
            int total = 0;
            Criteria c = new Criteria();
            c.add(Condition.AND, tbHinhAnhColumns.MaSP, Condition.EQUAL, int.Parse(lit_masp.Text));
            tbHinhAnhCollection haCollection = tbHinhAnhBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
                tbHinhAnhColumns.MaHA.ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = haCollection;
            Repeater1.DataBind();
            Pager.ItemCount = total;
            if (total <= Pager.PageSize)
            {
                Pager.Visible = false;
            }
        }
        catch
        {
            Response.Redirect("~/Admin/SanPhamDanhSach.aspx");
        }
    }
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnh hinhanh = (tbHinhAnh)e.Item.DataItem;
            Image img = (Image)e.Item.FindControl("hinhanh");
            img.ImageUrl = "~/Admin/Photos/Thumb/" + hinhanh.HinhAnh.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
}
